package com.insta.formation.calendar.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "PERSON")
public class Person implements java.io.Serializable
{
	private static final long serialVersionUID = 7128615638557413264L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int ID;

	@Column(unique = true, nullable = false)
	private String email;
	@Column(nullable = false)
	private String firstName;
	@Column(nullable = false)
	private String lastName;

	 @OneToMany(mappedBy = "creator", fetch = FetchType.LAZY, cascade =
	 CascadeType.ALL)
//	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//	@JoinColumn(name = "Creator_FK")
	private Set<Event> events;

	@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private Set<Calendar> calendars;

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = com.insta.formation.calendar.model.Event.class)
	@JoinTable(name = "EVENT_PERSON", joinColumns = @JoinColumn(name = "PERSON_ID"), inverseJoinColumns = @JoinColumn(name = "EVENT_ID"))
	private Set<Event> eventGuestList;
	
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = com.insta.formation.calendar.model.Calendar.class)
	@JoinTable(name = "CALENDAR_PERSON", joinColumns = @JoinColumn(name = "PERSON_ID"), inverseJoinColumns = @JoinColumn(name = "CALENDAR_ID"))
	private Set<Calendar> calendarGuestList;

	public Person() {
		events = new HashSet<Event>();
		eventGuestList = new HashSet<Event>();
		calendars = new HashSet<Calendar>();
	}

	/**
	 * Get the email of the person
	 * 
	 * @return - email of the person
	 */
	public String getEmail()
	{
		return this.email;
	}

	/**
	 * Set the email of the person
	 * 
	 * @param email
	 *            - email of the person
	 */
	public void setEmail(String email)
	{
		this.email = email;
	}

	/**
	 * Retourne l'identifiant de la personne
	 * 
	 * @return - identifiant de la personne
	 */
	public int getID()
	{
		return this.ID;
	}

	/**
	 * Modifie l'identifiant de la personne
	 * 
	 * @param iD
	 *            - identifiant de la personne
	 */
	public void setID(int iD)
	{
		this.ID = iD;
	}

	/**
	 * Retourne le prenom de la personne
	 * 
	 * @return - prenom de la personne
	 */
	public String getFirstName()
	{
		return firstName;
	}

	/**
	 * Modifie le prenom de la personne
	 * 
	 * @param firstName
	 *            - prenom de la personne
	 */
	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}

	/**
	 * Retourne le nom de la personne
	 * 
	 * @return - nom de la personne
	 */
	public String getLastName()
	{
		return lastName;
	}

	/**
	 * Modifie le nom de la personne
	 * 
	 * @param lastName
	 *            - nom de la personne
	 */
	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

	/**
	 * Retou
	 * 
	 * @return
	 */
	public Set<Event> getEvents()
	{
		return this.events;
	}

	public void setEvents(Set<Event> events)
	{
		this.events = events;
	}

	public void addEvent(Event event)
	{
		this.events.add(event);
	}

	/**
	 * Retourne les Events ou la personne est invit�e
	 * 
	 * @return - Events ou la personne est invit�e
	 */
	public Set<Event> getEventGuestList()
	{
		return eventGuestList;
	}

	/**
	 * Modifie la liste des Events ou la personne est invit�s
	 * 
	 * @param eventGuestList
	 *            - Events ou la personne est invit�s
	 */
	public void setEventGuestList(Set<Event> eventGuestList)
	{
		this.eventGuestList = eventGuestList;
	}

	public Set<Calendar> getCalendars()
	{
		return this.calendars;
	}

	public void setCalendars(Set<Calendar> calendars)
	{
		this.calendars = calendars;
	}
	
//	public Set<Calendar> getCalendarGuestList() {
//		return calendarGuestList;
//	}
//
//	public void setCalendarGuestList(Set<Calendar> calendarGuestList) {
//		this.calendarGuestList = calendarGuestList;
//	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((events == null) ? 0 : events.hashCode());
		result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Person other = (Person) obj;
		if (events == null) {
			if (other.events != null) return false;
		}
		else if (!events.equals(other.events)) return false;
		if (firstName == null) {
			if (other.firstName != null) return false;
		}
		else if (!firstName.equals(other.firstName)) return false;
		if (lastName == null) {
			if (other.lastName != null) return false;
		}
		else if (!lastName.equals(other.lastName)) return false;
		return true;
	}

	@Override
	public String toString() {
		return "Person [ID=" + ID + ", email=" + email + ", firstName="
				+ firstName + ", lastName=" + lastName + ", events=" + events
				+ ", calendars=" + calendars + ", eventGuestList="
				+ eventGuestList + "]";
	}

	
}